package dbtest;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

/**
 *
 * @author Henrik
 */
public class Piece {

    public static enum Color {

        WHITE, BLACK
    }

    public static enum Type {

        KING,
        QUEEN,
        BISHOP,
        KNIGHT,
        ROOK,
        PAWN
    }
    private int col;
    private int row;
    private Color color;
    private Type type;
    private BufferedImage img;
    private String pieceName;

    public Piece(int x, int y, Color color, Type type, String pieceName) {
        this.col = x;
        this.row = y;
        this.color = color;
        this.type = type;
        this.pieceName = pieceName;

        switch (type) {
            case KING:
            case QUEEN:
            case BISHOP:
            case KNIGHT:
            case ROOK:
            case PAWN:
                try {
                    String file = (color == Color.BLACK)
                            ? "pieces" + File.separator + "b_pawn.png" : "pieces" + File.separator + "w_pawn.png";
                    img = ImageIO.read(new File(file));
                } catch (IOException ex) {
                    Logger.getLogger(Piece.class.getName()).log(Level.SEVERE, null, ex);
                }
                break;
        }
    }

    public boolean canMove(Tile tile) {
        switch (type) {
            case KING:
            case QUEEN:
            case BISHOP:
            case KNIGHT:
            case ROOK:
            case PAWN:
                switch (color) {
                    case WHITE:
                        if (tile.row - this.row == 1
                                && tile.col == this.col && tile.getPiece() == null) {
                            return true;
                        }
                        break;
                    case BLACK:
                        if (this.row - tile.row == 1
                                && tile.col == this.col && tile.getPiece() == null) {
                            return true;
                        }
                        break;
                }
                break;
        }
        return false;
    }

    public void move(Tile tile) {
        this.col = tile.col;
        this.row = tile.row;
        tile.setPiece(this);
    }

    public int getCol() {
        return col;
    }

    public int getRow() {
        return row;
    }

    public BufferedImage getImage() {
        return img;
    }

    public String getPieceName() {
        return pieceName;
    }

    public Color getColor() {
        return color;
    }

    public String getPos() {
        String s = "";
        switch (col) {
            case 0:
                s = s.concat("a");
                break;
            case 1:
                s = s.concat("b");
                break;
            case 2:
                s = s.concat("c");
                break;
            case 3:
                s = s.concat("d");
                break;
            case 4:
                s = s.concat("e");
                break;
            case 5:
                s = s.concat("f");
                break;
            case 6:
                s = s.concat("g");
                break;
            case 7:
                s = s.concat("h");
                break;
        }
        s = s.concat("" + (row + 1));
        System.out.println("pos: " + s);
        return s;
    }

    @Override
    public String toString() {
        String s;
        switch (type) {
            case PAWN:
                s = "Pawn (";
                break;
            default:
                s = "nein ";
        }
        s += (col + ", " + row + ")");
        return s;
    }
}